package Test;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter;
import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;
import org.w3c.dom.NodeList;

/**
 * @TODO Se borra nico?
 *
 * @author BIPiane
 */
public class SVGApplication {

    public static void main(String[] args) {
        JFrame f = new JFrame("Batik");
        SVGApplication app = new SVGApplication(f);
        f.getContentPane().add(app.createComponents());
        f.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        f.setSize(400, 400);
        f.setVisible(true);
    }
    JFrame frame;
    JButton button = new JButton("Cargar...");
    JLabel label = new JLabel();
    JSVGCanvas svgCanvas = new JSVGCanvas();

    public SVGApplication(JFrame f) {
        frame = f;
    }

    public JComponent createComponents() {
        final JPanel panel = new JPanel(new BorderLayout());
        JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
        p.add(button);
        p.add(label);
        panel.add(p, BorderLayout.NORTH);
        panel.add(svgCanvas, BorderLayout.CENTER);

        // Set the button action.
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                JFileChooser fc = new JFileChooser(".");
                int choice = fc.showOpenDialog(panel);
                if (choice == JFileChooser.APPROVE_OPTION) {
                    File f = fc.getSelectedFile();
                    try {
                        svgCanvas.setURI(f.toURL().toString());
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

        // Set the JSVGCanvas listeners.
        svgCanvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {

            @Override
            public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
                label.setText("Cargango documento...");
            }

            @Override
            public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
                label.setText("Documento cargado.");
            }
        });

        svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {

            @Override
            public void gvtBuildStarted(GVTTreeBuilderEvent e) {
                label.setText("Build Started...");
            }

            @Override
            public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
                label.setText("Build Done.");
                frame.pack();
            }
        });

        svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {

            @Override
            public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
                label.setText("Randerizado comenzado...");
            }

            @Override
            public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
                label.setText("");
                NodeList lista = svgCanvas.getSVGDocument().getElementsByTagName("path");
                for (int i = 0; i < lista.getLength(); i++) {
                    String puntosSVG = lista.item(i).getAttributes().getNamedItem("d").getNodeValue();

                    System.out.println("\nOriginal");
                    System.out.println(puntosSVG);
                    getPuntosRelativos(puntosSVG);
                    System.out.println();
                    getPuntosAbsolutos(puntosSVG);
                }
            }
        });

        return panel;
    }

    private void getPuntosRelativos(String puntosSVG) {
        System.out.println("\ngetPuntosRelativos");
        String subNumero[] = puntosSVG.split(" ");
        String numeroJ = "m";
        for (int j = 0; j < subNumero.length; j++) {
            numeroJ = subNumero[j];
            if (Character.isLetter(numeroJ.charAt(0))) {
                numeroJ = numeroJ.substring(1);
            } else {
                if (Character.isLetter(numeroJ.charAt(numeroJ.length() - 1))) {
                    numeroJ = numeroJ.substring(0, numeroJ.length() - 1);
                }
            }
            System.out.print(numeroJ + " ");
        }
    }

    private void getPuntosAbsolutos(String puntosSVG) {
        System.out.println("\ngetPuntosAbsolutos");
        String subNumero[] = puntosSVG.split(" ");
        int px = 0;
        int py = 0;

        String resp = "M";
        for (int j = 0; j < subNumero.length; j++) {
            String numeroJ = subNumero[j];
            if (Character.isLetter(numeroJ.charAt(0))) {
                numeroJ = numeroJ.substring(1);
            } else {
                if (Character.isLetter(numeroJ.charAt(numeroJ.length() - 1))) {
                    numeroJ = numeroJ.substring(0, numeroJ.length() - 1);
                }
            }
            if (j % 2 == 0) {
                px += Integer.parseInt(numeroJ);
                resp = resp + px + " ";
            } else {
                py += Integer.parseInt(numeroJ);
                resp = resp + py + " ";
            }
        }
        System.out.print(resp);
    }
}
